﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity.Core;
using YFIN.View.Controls;
using YFIN.View.User;
using YFIN.View.UserServiceRef;
using YFIN.View.Helper;

namespace YFIN.View.ViewModels
{
    public class RegisterVM : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region 属性
        /// <summary>
        /// 用户名
        /// </summary>
        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (_userName != value)
                {
                    _userName = value;
                    NotifyPropertyChanged("UserName");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string _password;
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    NotifyPropertyChanged("Password");
                }
            }
        }
        /// <summary>
        /// 密码test
        /// </summary>
        public Register register { get; set; }

        /// <summary>
        /// 验证码
        /// </summary>
        private WriteableBitmap _code;
        public WriteableBitmap Code
        {
            get
            {
                return _code;
            }
            set
            {
                if (_code != value)
                {
                    _code = value;
                    NotifyPropertyChanged("Code");
                }
            }
        }

        /// <summary>
        /// 输入的验证码
        /// </summary>
        private string _codeStr;
        public string CodeStr
        {
            get
            {
                return _codeStr;
            }
            set
            {
                if (_codeStr != value)
                {
                    _codeStr = value;
                    NotifyPropertyChanged("CodeStr");
                }
            }
        }

        /// <summary>
        /// 邮箱
        /// </summary>
        private string _email;
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                if (_email != value)
                {
                    _email = value;
                    NotifyPropertyChanged("Email");
                }
            }
        }

        /// <summary>
        /// 绑定到的实现登录操作的命令
        /// </summary>
        public ICommand RegisterCommand { get; private set; }
        public ICommand ChangeCodeCommand { get; private set; }
        #endregion

        private string _codeStrTrue;
        public RegisterVM()
        {
            RegisterCommand = new ActionCommand(Register);
            ChangeCodeCommand = new ActionCommand(ChangeCode);

            var c = VerifyCode.GetVerifyCode();
            Code = c.Value;
            _codeStrTrue = c.Key;
        }

        public void ChangeCode()
        {
            var c = VerifyCode.GetVerifyCode();
            Code = c.Value;
            _codeStrTrue = c.Key;
        }

        public void Register()
        {
            register.UserBox.ClearValidationError();
            register.PasswordBox.ClearValidationError();
            register.EmailBox.ClearValidationError();
            bool isValid = true;
            if (register.UserBox.Text.Length == 0)
            {
                register.UserBox.ShowValidationError("用户名不能为空");
                isValid = false;
            }
            if (register.PasswordBox.Password.Length <= 5)
            {
                register.PasswordBox.ShowValidationError("密码必须大于等于6位");
                isValid = false;
            }
            if (register.PasswordBox1.Password.Length <= 5)
            {
                register.PasswordBox1.ShowValidationError("密码必须大于等于6位");
                isValid = false;
            }
            if (register.EmailBox.Text.Length ==0)
            {
                register.EmailBox.ShowValidationError("邮箱不能为空");
                isValid = false;
            }
            if (register.PasswordBox.Password != register.PasswordBox1.Password)
            {
                register.PasswordBox.ShowValidationError("密码不相同");
                register.PasswordBox1.ShowValidationError("密码不相同");
                isValid = false;
            }
            if (CodeStr != _codeStrTrue)
            {
                register.tb.ShowValidationError("验证码不正确");
                isValid = false;
            }
            if (isValid)
            {
                var user = new ModelUser
                {
                    UserName = UserName,
                    Password = register.PasswordBox.Password.GetHashCode().ToString(),
                    Email = Email,
                    RemindWay = 0,
                    PublicLevel = 0
                };
                var client = new UserServiceClient();
                client.AddCompleted += client_AddCompleted;
                client.AddAsync(user);
                Stage.Register.Close();
            }
        }

        private void client_AddCompleted(object sender, UserServiceRef.AddCompletedEventArgs e)
        {
            if(e.Result)
            {
                Stage.Login(new ModelUser
                                {
                                    UserName = UserName,
                                    Password = register.PasswordBox.Password,
                                    Email = Email,
                                    RemindWay = 0,
                                    PublicLevel = 0
                                });
            }
            else
            {
                MessageBox.Show("注册失败");
            }
        }
    }
}
